package com.ponxu.utils;

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collection;

/**
 * 工具大集合
 * 
 * @author xwz
 */
public class Lang {
	private static final String DEFAULT_STRING = "";

	/** =====异常 begin====================================== */
	/**
	 * 抛出运行时异常
	 */
	public static void makeThrow() {
		RuntimeException e = new RuntimeException();
		throw correctStackTrace(e);
	}

	/**
	 * 抛出运行时异常
	 */
	public static void makeThrow(String message) {
		RuntimeException e = new RuntimeException(message);
		throw correctStackTrace(e);
	}

	/**
	 * 抛出运行时异常
	 */
	public static void makeThrow(Throwable t) {
		RuntimeException e = new RuntimeException(t);
		throw correctStackTrace(e);
	}

	/**
	 * 抛出运行时异常
	 */
	public static void makeThrow(String message, Throwable t) {
		RuntimeException e = new RuntimeException(message, t);
		throw correctStackTrace(e);
	}

	/**
	 * 移除 Lang层堆栈信息
	 * 
	 * @param e
	 * @return
	 */
	private static RuntimeException correctStackTrace(RuntimeException e) {
		StackTraceElement[] s = e.getStackTrace();
		e.setStackTrace(Arrays.copyOfRange(s, 1, s.length));
		return e;
	}

	/** =====异常 end====================================== */

	/**
	 * 判断对方像是否为空<br>
	 * String: null or " " 视为空<br>
	 * 集合或者数组: size == 0, null视为空
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {
		boolean empty = false;
		if (obj != null) {
			if (obj instanceof String) {
				empty = ((String) obj).trim().equals("");
			} else if (obj instanceof Collection) {
				empty = ((Collection<?>) obj).size() == 0;
			} else if (obj.getClass().isArray()) {
				empty = Array.getLength(obj) == 0;
			}
		} else {
			empty = true;
		} // end if (obj != null)

		return empty;
	}

	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}

	/**
	 * 若字符串为空,则返回指定的字符串
	 * 
	 * @param str
	 * @param defaultstr
	 * @return
	 */
	public static String defaultString(String str, String defaultstr) {
		return isEmpty(str) ? defaultstr : str;
	}

	/**
	 * 若字符串为空,则返回""
	 * 
	 * @param str
	 * @return
	 */
	public static String defaultString(String str) {
		return defaultString(str, DEFAULT_STRING);
	}

	/**
	 * 是否数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		if (str == null) {
			return false;
		}
		int sz = str.length();
		for (int i = 0; i < sz; ++i) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	public static boolean isNotNumeric(String str) {
		return !isNumeric(str);
	}

	/**
	 * 首先字母大写
	 * 
	 * @param str
	 * @return
	 */
	public static String firstUpperCase(String str) {
		if (isEmpty(str))
			return str;
		return str.substring(0, 1).toUpperCase() + str.substring(1);
	}

	/**
	 * md5 字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String md5(String str) {
		String ret = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			ret = new BigInteger(1, md.digest()).toString(16);
		} catch (NoSuchAlgorithmException e) {
			ret = null;
		}

		return ret;
	}

	/**
	 * 过滤html
	 * 
	 * @param str
	 * @return
	 */
	public static String htmlFilter(String str) {
		str = str.replaceAll("&", "&amp;");
		str = str.replaceAll("<", "&lt;");
		str = str.replaceAll(">", "&gt;");
		str = str.replaceAll("\"", "&quot;");
		return str;
	}

	public static int len(Object obj) {
		if (obj != null) {
			Class<?> clazz = obj.getClass();
			if (clazz.isArray()) {
				return Array.getLength(obj);
			} else if (obj instanceof Collection<?>) {
				((Collection<?>) obj).size();
			}
		}
		return 0;
	}

	public static boolean convertBoolean(String str) {
		if (Lang.isNotEmpty(str)) {
			str = str.trim();
			if ("true".equalsIgnoreCase(str) || "yes".equalsIgnoreCase(str)
					|| "1".equalsIgnoreCase(str) || "on".equalsIgnoreCase(str)) {
				return true;
			}
		}
		return false;
	}

	public static int charCountInString(String src, char c) {
		if (Lang.isEmpty(src)) {
			return 0;
		}

		int count = 0;
		char[] all = src.toCharArray();
		for (char c2 : all) {
			if (c2 == c)
				count++;
		}

		return count;
	}
}
